package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.Msg;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetMealDto;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.SetMeal;
import com.itheima.reggie.entity.SetMealDish;
import com.itheima.reggie.service.SetMealService;
import com.itheima.reggie.service.impl.SetMealServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    SetMealService setMealService;

    @GetMapping("/dish/{id}")
    public R getDishId(@PathVariable Long id) {
        List<SetMealDish> id1 = setMealService.getId(id);
        return R.success(id1);
    }

    @GetMapping("/list")
    public R getId(Long categoryId, Integer status) {
        List<SetMealDto> list = setMealService.list(categoryId, status);
        return R.success(list);
    }

    @PostMapping("/status/{id}")
    public R status(@PathVariable int id, Long[] ids) {
        boolean a = false;
        for (Long aLong : ids) {
            LambdaQueryWrapper<SetMeal> ds = new LambdaQueryWrapper<>();
            ds.eq(SetMeal::getId, aLong);
            SetMeal SetMeal = new SetMeal();
            SetMeal.setStatus(id);
            a = setMealService.update(SetMeal, ds);
        }
        if (a) {
            return R.success(Msg.UPDATE_SUCCESS);
        }
        return R.error(Msg.UPDATE_ERROR);
    }

    @DeleteMapping
    public R delete(Long[] ids) {
        boolean delete = setMealService.delete(Arrays.asList(ids));
        if (delete) {
            return R.success(Msg.DELETE_SUCCESS);
        }
        return R.error(Msg.DELETE_ERROR);
    }

    @PutMapping
    public R update(@RequestBody SetMealDto SetMealDto) {
        boolean update = setMealService.update(SetMealDto);
        if (update) {
            return R.success(Msg.UPDATE_SUCCESS);
        }
        return R.error(Msg.UPDATE_ERROR);
    }

    @PostMapping
    public R add(@RequestBody SetMealDto SetMealDto) {
        boolean add = setMealService.add(SetMealDto);
        if (add) {
            return R.success(Msg.ADD_SUCCESS);
        }
        return R.error(Msg.ADD_ERROR);
    }

    @GetMapping("page")
    public R pageAll(PageParam pageParam) {
        IPage<SetMealDto> pageAll = setMealService.pageAll(pageParam);
        if (pageAll != null && pageAll.getRecords().size() > 0) {
            return R.success(pageAll);
        }
        return R.error(Msg.FIND_ERROR);
    }
}
